//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DeltaVolQuote.h"
using namespace Cephei::QL::Experimental::Fx;
#include <gen/QL/Quote.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (Cephei::QL::IQuote^ vol, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum deltaType, Double maturity, QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum atmType) : CQuote(CDeltaVolQuote::typeid)
{
    CQuote^ _Cvol;
    try
    {
#ifdef HANDLE
        _phDeltaVolQuote = NULL;
#endif
        _Cvol = safe_cast<CQuote^> (vol);
        _Cvol->Lock();
        Handle<QuantLib::Quote>& _vol = static_cast<Handle<QuantLib::Quote>&> (_Cvol->GetHandle ()); 
        QuantLib::DeltaVolQuote::DeltaType _deltaType = (QuantLib::DeltaVolQuote::DeltaType)deltaType ;
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //d
        QuantLib::DeltaVolQuote::AtmType _atmType = (QuantLib::DeltaVolQuote::AtmType)atmType ;
        _ppDeltaVolQuote = new boost::shared_ptr<QuantLib::DeltaVolQuote> (new QuantLib::DeltaVolQuote ( _vol,  _deltaType,  _maturity,  _atmType ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvol != nullptr) _Cvol->Unlock();
    }
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (Double delta, Cephei::QL::IQuote^ vol, Double maturity, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum deltaType) : CQuote(CDeltaVolQuote::typeid)
{
    CQuote^ _Cvol;
    try
    {
#ifdef HANDLE
        _phDeltaVolQuote = NULL;
#endif
        QuantLib::Real _delta = (QuantLib::Real)ValueHelper::Convert (delta); //d
        _Cvol = safe_cast<CQuote^> (vol);
        _Cvol->Lock();
        Handle<QuantLib::Quote>& _vol = static_cast<Handle<QuantLib::Quote>&> (_Cvol->GetHandle ()); 
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //d
        QuantLib::DeltaVolQuote::DeltaType _deltaType = (QuantLib::DeltaVolQuote::DeltaType)deltaType ;
        _ppDeltaVolQuote = new boost::shared_ptr<QuantLib::DeltaVolQuote> (new QuantLib::DeltaVolQuote ( _delta,  _vol,  _maturity,  _deltaType ));
        SetQuote (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvol != nullptr) _Cvol->Unlock();
    }
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (boost::shared_ptr<QuantLib::DeltaVolQuote>& childNative, Object^ owner) : CQuote(CDeltaVolQuote::typeid)
{
#ifdef HANDLE
	_phDeltaVolQuote = NULL;
#endif
	_ppDeltaVolQuote = &childNative;
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (QuantLib::DeltaVolQuote& childNative, Object^ owner) : CQuote(CDeltaVolQuote::typeid)
{
#ifdef HANDLE
	_phDeltaVolQuote = NULL;
#endif
	_ppDeltaVolQuote = new boost::shared_ptr<QuantLib::DeltaVolQuote> (&childNative);
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
    _DeltaVolQuoteOwner = owner;
    _QuoteOwner = owner;
}

Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (CDeltaVolQuote^ copy) : CQuote(CDeltaVolQuote::typeid)
{
#ifdef HANDLE
	_phDeltaVolQuote = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDeltaVolQuote = new boost::shared_ptr<QuantLib::DeltaVolQuote> (copy->GetShared());
        _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
    }
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (PLATFORM::Type^ t) : CQuote(CDeltaVolQuote::typeid)
{
#ifdef HANDLE
	_phDeltaVolQuote = NULL;
#endif
	if (!t->IsSubclassOf(CDeltaVolQuote::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (QuantLib::Handle<QuantLib::DeltaVolQuote>& childNative, Object^ owner)  : CQuote(CDeltaVolQuote::typeid)
{
	_phDeltaVolQuote = &childNative;
	_ppDeltaVolQuote = &static_cast<boost::shared_ptr<QuantLib::DeltaVolQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
    _DeltaVolQuoteOwner = owner;
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (QuantLib::Handle<QuantLib::DeltaVolQuote> childNative)  : CQuote(CDeltaVolQuote::typeid)
{
	_phDeltaVolQuote = &childNative;
	_ppDeltaVolQuote = &static_cast<boost::shared_ptr<QuantLib::DeltaVolQuote>>(childNative.currentLink());
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Fx::CDeltaVolQuote::CDeltaVolQuote (QuantLib::DeltaVolQuote childNative)  : CQuote(CDeltaVolQuote::typeid)
{
#ifdef HANDLE
	_phDeltaVolQuote = NULL;
#endif
	_ppDeltaVolQuote = new boost::shared_ptr<QuantLib::DeltaVolQuote> (new QuantLib::DeltaVolQuote (childNative));
    _ppQuote = new boost::shared_ptr<QuantLib::Quote> (boost::dynamic_pointer_cast<QuantLib::Quote> (*_ppDeltaVolQuote));
}
#endif

Cephei::QL::Experimental::Fx::CDeltaVolQuote::~CDeltaVolQuote ()
{
    if (_ppDeltaVolQuote != NULL)
    {
	    delete _ppDeltaVolQuote;
        _ppDeltaVolQuote = NULL;
    }
}
Cephei::QL::Experimental::Fx::CDeltaVolQuote::!CDeltaVolQuote ()
{
    if (_ppDeltaVolQuote != NULL)
    {
	    delete _ppDeltaVolQuote;
    }
}
QuantLib::DeltaVolQuote& Cephei::QL::Experimental::Fx::CDeltaVolQuote::GetReference ()
{
    if (_ppDeltaVolQuote == NULL) throw REFNEW NativeNullException ();
	return **_ppDeltaVolQuote;
}
boost::shared_ptr<QuantLib::DeltaVolQuote>& Cephei::QL::Experimental::Fx::CDeltaVolQuote::GetShared ()
{
    if (_ppDeltaVolQuote == NULL) throw REFNEW NativeNullException ();
	return *_ppDeltaVolQuote;
}
QuantLib::DeltaVolQuote* Cephei::QL::Experimental::Fx::CDeltaVolQuote::GetPointer ()
{
    if (_ppDeltaVolQuote == NULL) throw REFNEW NativeNullException ();
	return &**_ppDeltaVolQuote;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DeltaVolQuote>& Cephei::QL::Experimental::Fx::CDeltaVolQuote::GetHandle ()
{
	if (_phDeltaVolQuote == NULL)
	{
		_phDeltaVolQuote = new Handle<QuantLib::DeltaVolQuote> (*_ppDeltaVolQuote);
	}
	return *_phDeltaVolQuote;
}
#endif
bool Cephei::QL::Experimental::Fx::CDeltaVolQuote::HasNative () 
{
	return (_ppDeltaVolQuote != NULL);
}

QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum Cephei::QL::Experimental::Fx::CDeltaVolQuote::AtmType::get ()
{
    try
    {
    	QuantLib::DeltaVolQuote::AtmType _rv = (QuantLib::DeltaVolQuote::AtmType)(*_ppDeltaVolQuote)->atmType ( );   
        QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum _nrv = (QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CDeltaVolQuote::Delta::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDeltaVolQuote)->delta ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum Cephei::QL::Experimental::Fx::CDeltaVolQuote::DeltaType::get ()
{
    try
    {
    	QuantLib::DeltaVolQuote::DeltaType _rv = (QuantLib::DeltaVolQuote::DeltaType)(*_ppDeltaVolQuote)->deltaType ( );   
        QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum _nrv = (QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Fx::CDeltaVolQuote::IsValid::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppDeltaVolQuote)->isValid ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CDeltaVolQuote::Maturity::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppDeltaVolQuote)->maturity ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Fx::IDeltaVolQuote^ Cephei::QL::Experimental::Fx::CDeltaVolQuote::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppDeltaVolQuote)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Fx::CDeltaVolQuote::Value::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppDeltaVolQuote)->value ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Fx::IDeltaVolQuote^ Cephei::QL::Experimental::Fx::CDeltaVolQuote_Factory::Create (Cephei::QL::IQuote^ vol, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum deltaType, Double maturity, QL::Experimental::Fx::DeltaVolQuote::AtmTypeEnum atmType)
{
    return REFNEW CDeltaVolQuote ( vol,  deltaType,  maturity,  atmType);
}
Cephei::QL::Experimental::Fx::IDeltaVolQuote^ Cephei::QL::Experimental::Fx::CDeltaVolQuote_Factory::Create (Double delta, Cephei::QL::IQuote^ vol, Double maturity, QL::Experimental::Fx::DeltaVolQuote::DeltaTypeEnum deltaType)
{
    return REFNEW CDeltaVolQuote ( delta,  vol,  maturity,  deltaType);
}
